﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EssenceAnalytics.Data.EntityModel;
using EssenceAnalytics.Data.Interfaces;

namespace EssenceAnalytics.Data.Repositories
{
    /// <summary>
    /// Newsletter Repository
    /// </summary>
    public class NewsletterRepository : INewsletterRepository
    {
        readonly EssenceAnalyticsEntities _db = new EssenceAnalyticsEntities();

        /// <summary>
        /// Get Newsletter Details by Title
        /// </summary>
        /// <param name="strQuery"></param>
        /// <returns></returns>
        public IQueryable<Newsletter> FindNewsletterByName(string strQuery)
        {
            return (IQueryable<Newsletter>) _db.Newsletters.Where(d => d.Name.Contains(strQuery));
        }

        /// <summary>
        /// Get Newsletter Details by Email
        /// </summary>
        /// <param name="strQuery"></param>
        /// <returns></returns>
        public IQueryable<Newsletter> FindNewsletterByEmail(string strQuery)
        {
            return (IQueryable<Newsletter>) _db.Newsletters.Where(d => d.Email.Contains(strQuery));
        }

        /// <summary>
        /// Get All Newsletter
        /// </summary>
        /// <returns></returns>
        public IQueryable<Newsletter> FindAllNewsletter()
        {
            return (IQueryable<Newsletter>) _db.Newsletters;
        }

        /// <summary>
        /// Get Newsletter by Id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Newsletters GetNewsletterById(int id)
        {
            return _db.Newsletters.SingleOrDefault(d => d.NewsletterId == id);
        }

        /// <summary>
        /// Add Newsletter in the Database
        /// </summary>
        /// <param name="newsletter"></param>
        public void Add(Newsletters newsletter)
        {
            _db.Newsletters.AddObject(newsletter);
        }

        /// <summary>
        /// Delete Newsletter from Database
        /// </summary>
        /// <param name="newsletter"></param>
        public void Delete(Newsletters newsletter)
        {
            _db.Newsletters.DeleteObject(newsletter);
        }

        /// <summary>
        /// Save Newsletter in the database
        /// </summary>
        public void Save()
        {
            _db.SaveChanges();
        }
    }
}